package model

import business.*
import enums.Direction
import javafx.scene.canvas.GraphicsContext
import org.itheima.kotlin.game.core.Painter
import java.awt.Image
import java.io.File
import java.io.ObjectInputFilter
import kotlin.concurrent.thread

/**
 * 子弹类
 * @param direction:子弹方向
 * @param create:通过子弹的宽高得到子弹初始位置信息
 */
class Bullet(override var owner:IView?,override var direction: Direction,create:(w:Int,h:Int)-> Pair<Int,Int>):AutoMove,Destroyable,Attackable,Sufferable{
    override var blood: Int=1

    override fun notifySuffer(attack: Attackable): Array<IView>? {
        isDestroyable=true
        return null
    }

    private var img_path:String
    private lateinit var size:Array<Int>
    init {
        img_path=when(direction){
            Direction.UP -> "img/shot_top.gif"
            Direction.DOWN -> "img/shot_bottom.gif"
            Direction.LEFT -> "img/shot_left.gif"
            Direction.RIGHT -> "img/shot_right.gif"
            else -> throw Exception("子弹方向异常")
        }
        size=Painter.size(img_path)
    }
    override var width:Int=size[0]
    override var height:Int=size[1]
    var pair:Pair<Int,Int>
    init {
        //pair=create.invoke(width,height)
        pair=create(width,height)
    }
    override var x: Int= pair.first
    override var y: Int=pair.second

    override fun draw() {
        if(x<0||y<0||x>Config.gameWidth||y>Config.gameHeight){
            return
        }
        Painter.drawImage(img_path,x,y)
    }

    override val speed=5
    override val attackPower: Int=1
    var  isDestroyable:Boolean=false
    /*
    * 子弹移动
    */
    override fun autoMove(){
        when(direction){
            Direction.UP -> y-=speed
            Direction.DOWN -> y+=speed
            Direction.LEFT -> x-=speed
            Direction.RIGHT -> x+=speed
        }
    }

    override fun isDestroy(): Boolean {
        if(isDestroyable) return true
        if(x<0||y<0||x>Config.gameWidth||y>Config.gameHeight){
            return true
        }
        return false
    }

    override fun isCollision(x1: Int, y1: Int, w1: Int, h1: Int, x2: Int, y2: Int, w2: Int, h2: Int): Boolean {
        return checkCollision(x1,y1,w1,h1, x2, y2, w2, h2)
    }

    override fun notifyAttack(suffer: Sufferable) {
        if(suffer==owner){
            return
        }
        isDestroyable=true
    }
}